import 'package:get/get.dart';
import 'package:nyh_motorcade_app/constant/constant.dart';
import 'package:nyh_motorcade_app/entity/motorcade_entity.dart';
import 'package:nyh_motorcade_app/http/http_address.dart';
import 'package:nyh_motorcade_app/http/http_manager.dart';
import 'package:nyh_motorcade_app/routes/routes.dart';
import 'package:nyh_motorcade_app/utils/sp_utils.dart';
import 'package:nyh_motorcade_app/utils/toast_utils.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class MotorcadeAccountController extends GetxController {
  RefreshController refreshController = RefreshController();
  MultiState multiState = MultiState.loading;
  String motorcadeId = "";
  String motorcadeName = "";
  bool showMotorcadeMenu = false;
  List<MotorcadeEntity> motorcadeList = [];
  List<MotorcadeEntity> motorcadeShowList = [];
  List<MotorcadeEntity> motorcadeFilterList = [];
  bool permissionTransferMotorcade = false;

  @override
  Future<void> onReady() async {
    super.onReady();
    permissionTransferMotorcade = await SPUtils.getBool(SPUtils.PERMISSION_TRANSFER_MOTORCADE);
    getMotorcadeList();
  }

  Future<void> onRefresh() async {
    getMotorcadeList();
  }

  void onRetry() {
    multiState = MultiState.loading;
    update();
    getMotorcadeList();
  }

  Future<void> goToRapidTransferPage(String? id, String? name, double? amount) async {
    if (id == null || id.isEmpty) {
      return;
    }
    dynamic data = await Get.toNamed(Routes.rapidTransfer, arguments: {
      Constant.PARAM_RAPID_TRANSFER_FROM: Constant.RAPID_TRANSFER_FROM_MOTORCADE,
      Constant.PARAM_ACCOUNT_ID: id,
      Constant.PARAM_ACCOUNT_NAME: name ?? "",
      Constant.PARAM_ACCOUNT_AMOUNT: amount ?? 0.0,
    });
    if (data != null) {
      multiState = MultiState.loading;
      update();
      getMotorcadeList();
    }
  }

  Future<void> getMotorcadeList() async {
    httpManager.post(
      Get.context,
      HttpAddress.GET_MOTORCADE_LIST,
      {"superId": await SPUtils.getString(SPUtils.COMPANY_ID)},
      isToast: true,
      onSuccess: (data) {
        if (data == null) {
          ToastUtils.toast("请求失败");
          if (refreshController.isRefresh) {
            refreshController.refreshFailed();
          }
          multiState = MultiState.fail;
          update();
          return;
        }
        if (data is List) {
          motorcadeList = data.map((element) {
            return MotorcadeEntity().fromJson((element as Map<String, dynamic>));
          }).toList();
          if (refreshController.isRefresh) {
            refreshController.refreshCompleted();
          }
          if (motorcadeList.isNotEmpty) {
            motorcadeList.forEach((element) {
              motorcadeShowList.add(element);
              motorcadeFilterList.add(element);
            });
            MotorcadeEntity motorcadeEntity = MotorcadeEntity();
            motorcadeEntity.id = "";
            motorcadeEntity.name = "全部";
            motorcadeFilterList.insert(0, motorcadeEntity);
          }
          multiState = motorcadeShowList.isNotEmpty ? MultiState.success : MultiState.empty;
          update();
        }
      },
      onFail: (code, msg) {
        if (refreshController.isRefresh) {
          refreshController.refreshFailed();
        }
        multiState = MultiState.fail;
        update();
      },
      onError: (msg) {
        if (refreshController.isRefresh) {
          refreshController.refreshFailed();
        }
        multiState = MultiState.error;
        update();
      },
    );
  }

  void setSelectMotorcade(int? value) {
    showMotorcadeMenu = false;
    if (value != null) {
      String id = motorcadeFilterList[value].id ?? "";
      motorcadeName = value != 0 ? (motorcadeFilterList[value].name ?? "") : "全部车队";
      if (motorcadeId != id) {
        motorcadeId = id;
        if (motorcadeList.isNotEmpty) {
          motorcadeShowList.clear();
          if (motorcadeId.isNotEmpty) {
            motorcadeList.forEach((element) {
              if (motorcadeId == element.id) {
                motorcadeShowList.add(element);
              }
            });
          } else {
            motorcadeList.forEach((element) {
              motorcadeShowList.add(element);
            });
          }
        }
      }
    }
    update();
  }

  void goToCashFlowPage(String? id) {
    if (id == null || id.isEmpty) {
      return;
    }
    Get.toNamed(
      Routes.cashFlow,
      arguments: {
        "sourceId": id,
        "accountType": Constant.MOTORCADE,
      },
    );
  }
}
